{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import MySQLdb;\n",
    "# 打开数据库连接\n",
    "db = MySQLdb.connect(\"localhost\", \"root\", \"123456\", \"lexian\", port=3306,charset='utf8' )\n",
    "\n",
    "# 使用cursor()方法获取操作游标 \n",
    "cursor = db.cursor()\n",
    "\n",
    "# 使用execute方法执行SQL语句\n",
    "cursor.execute(\"select * from browse\")\n",
    "\n",
    "# 使用 fetchone() 方法获取一条数据\n",
    "data = cursor.fetchone()\n",
    "\n",
    "print(data)\n",
    "print(type(data))\n",
    "\n",
    "# 关闭数据库连接\n",
    "db.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import MySQLdb;\n",
    "# 打开数据库连接\n",
    "db = MySQLdb.connect(\"localhost\", \"root\", \"123456\", \"lexian\", port=3306,charset='utf8' )\n",
    "\n",
    "# 使用cursor()方法获取操作游标 \n",
    "cursor = db.cursor()\n",
    "\n",
    "# 使用execute方法执行SQL语句\n",
    "cursor.execute(\"select * from browse\")\n",
    "\n",
    "# 使用 fetchone() 方法获取一条数据\n",
    "data = cursor.fetchall()\n",
    "\n",
    "for item in data:\n",
    "    print(str(item[0]))\n",
    "    print(item[0:])\n",
    "\n",
    "\n",
    "print(data)\n",
    "print(type(data))\n",
    "\n",
    "# 关闭数据库连接\n",
    "db.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INSERT INTO browse (commodity_no, user_id, browsetime) values (%s, %s, current_date) \n"
     ]
    }
   ],
   "source": [
    "import MySQLdb;\n",
    "# 打开数据库连接\n",
    "db = MySQLdb.connect(\"localhost\", \"root\", \"123456\", \"lexian\", port=3306,charset='utf8' )\n",
    "\n",
    "# 使用cursor()方法获取操作游标 \n",
    "cursor = db.cursor()\n",
    "\n",
    "# 使用execute方法执行SQL语句\n",
    "sql = \"\"\"INSERT INTO browse (commodity_no, user_id, browsetime) values (%s, %s, current_date) \"\"\" \n",
    "print(sql)\n",
    "try:\n",
    "    cursor.execute(sql, ('6946881700476', '146fe691-f6fd-4400-bbdb-d5835fb8207c'))\n",
    "\n",
    "    db.commit()\n",
    "except Exception as e:\n",
    "    db.rollback()\n",
    "    print(e)\n",
    "    \n",
    "\n",
    "# 关闭数据库连接\n",
    "db.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
